package com.zib.bilibili.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Toast;

import com.zib.bilibili.R;
import com.zib.bilibili.base.CommonAdapter;
import com.zib.bilibili.base.ViewHolder;
import com.zib.bilibili.base.adapter.BaseRcAdapterHelper;
import com.zib.bilibili.base.adapter.BaseRcQuickAdapter;
import com.zib.bilibili.bean.LiveBean;
import com.zib.bilibili.utils.ImageLoader;
import com.zib.bilibili.view.LiveActivity;

/**
 * Created by Administrator on 2016/7/6.
 */
public class LiveFragmentAdapter extends BaseRcQuickAdapter<LiveBean.DataBean.PartitionsBean, BaseRcAdapterHelper> {
    private Context context = null;

    public LiveFragmentAdapter(Context context, int layoutResId) {
        super(context, layoutResId);
        this.context = context;
    }

    @Override
    protected void convert(BaseRcAdapterHelper helper, final LiveBean.DataBean.PartitionsBean item) {
        ImageView imageView = helper.getImageView(R.id.imageView_live_classify);
        GridView gridView = helper.getGridView(R.id.gridView_item);
        helper.setText(R.id.textView_live_classify, item.getPartition().getName());
        ImageLoader.getInstance().displayImage(context, item.getPartition().getSub_icon().getSrc(), imageView);
        helper.setText(R.id.textView_live_num, "当前" + item.getPartition().getCount() + "个直播");
        gridView.setAdapter(new CommonAdapter<LiveBean.DataBean.PartitionsBean.LivesBean>(context, item.getLives(), R.layout.item_gridviewitem_live) {
            @Override
            public void convert(ViewHolder helper, LiveBean.DataBean.PartitionsBean.LivesBean item) {
                helper.setImageByUrl(R.id.imageView_live_image, item.getCover().getSrc());
                helper.setImageByUrl(R.id.imageView_live_icon, item.getOwner().getFace());
                helper.setText(R.id.textView_live_username, item.getOwner().getName());
                helper.setText(R.id.textView_live_count, item.getOnline() + "");
                helper.setText(R.id.textView_live_title, item.getTitle());
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putInt("roomid", item.getLives().get(position).getRoom_id());
                bundle.putString("playurl", item.getLives().get(position).getPlayurl());
                bundle.putString("iconurl", item.getLives().get(position).getOwner().getFace());
                intent.putExtras(bundle);
                intent.setClass(context, LiveActivity.class);
                context.startActivity(intent);
            }
        });
        setListViewHeightBasedOnChildren(gridView);
    }

    //重设ListView的高度
    public void setListViewHeightBasedOnChildren(GridView gridView) {

        // 获取ListView对应的Adapter

        ListAdapter listAdapter = gridView.getAdapter();

        if (listAdapter == null) {

            return;

        }

        int totalHeight = 0;

//        for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回数据项的数目
        if (listAdapter.getCount() <= 1) {
            View listItem = listAdapter.getView(0, null, gridView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight = listItem.getMeasuredHeight(); // 统计所有子项的总高度
        } else {
            View listItem = listAdapter.getView(0, null, gridView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight = listItem.getMeasuredHeight() * 2; // 统计所有子项的总高度
        }
//        }

        ViewGroup.LayoutParams params = gridView.getLayoutParams();

        params.height = totalHeight;

        // listView.getDividerHeight()获取子项间分隔符占用的高度

        // params.height最后得到整个ListView完整显示需要的高度

        gridView.setLayoutParams(params);

    }


}
